//============================================================================
// Name        : autosyncshell.cpp
// Author      : alex2stf
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "stn/FileUtils.h"
using namespace std;

string localRoot;
string remoteRoot;
string SSH;
string PASSWORD;
string user;
int port;

void giveValues(){
	 ObjectsFromFile *objects=new ObjectsFromFile("demo.txt");
	 localRoot=objects->getPropertyByName("LOCAL_ROOT");
	 SSH=objects->getPropertyByName("SSH");
	 PASSWORD=objects->getPropertyByName("PASSWORD");
	 remoteRoot=objects->getPropertyByName("REMOTE_ROOT");
	 user=objects->getPropertyByName("USER");
	 port=atoi(objects->getPropertyByName("PORT").c_str());
	 delete objects;
}

string composeSvnQuery(){
	string result;
	result.append("svn status ");
	result.append(localRoot);
	return result;
}


void processData(string ocommand){
	cout << "lucrez cu " << ocommand << endl;
	string::size_type indexOf=ocommand.find("/");
	string accuratePath;
	string relativeFilePath;
	        if(indexOf != std::string::npos) {
	                        accuratePath="/"+ocommand.substr(indexOf+1);
	                        relativeFilePath=accuratePath.replace(0, localRoot.length(), "");
	                        //processData(relativeFilePath);
	                       // commands.push_back(relativeFilePath);
	                        //cout << tdata << endl;
	                }

	        cout << "RELATIVE PATH: " << relativeFilePath << endl;






//	char *str1 = const_cast<char*>(relativeFilePath.c_str());
//	char str2[] = "/";
//	char * pnt;
//	pnt=strtok( str1, str2 );
//
//
//
//
//	cout << "-----" << pnt << endl;

	string command;
	command="expect -c 'spawn scp "+localRoot+relativeFilePath+" "+SSH+":"+remoteRoot+relativeFilePath+"; expect assword; send "+"\""+PASSWORD+"\n\"; interact'";

	//string builtInSh="./scpTool.sh LOCAL='"+localRoot+relativePath+"' "+remoteRoot+relativePath+" "+SSH+" "+PASSWORD;
	string builtInSh="LOCAL='"+localRoot+"' REMOTE='"+remoteRoot+"' RELATIVE_PATH='"+relativeFilePath+"' SSH="+SSH+" PASSWORD="+PASSWORD+" ./scpTool.sh";

char comanda[builtInSh.size()];
for(int a=0; a<(int) builtInSh.size(); a++){
	comanda[a]=builtInSh[a];
}
cout << "EXECUTE SHELL SCRIPT: " << endl;
cout.flush();


system(builtInSh.c_str());// unde c_str() nu altereaza stringul

}

int main() {
	giveValues();
	cout << "LOCAL ROOT:" << localRoot << endl;
	FileResult fileResult=stn_getStdOutFromCommand(composeSvnQuery());
	cout << "EXECUTE SVN COMMAND" << endl;
	//fileResult.printLines();


	for(int i=0; i< fileResult.getLines().size(); i++){
		//cout << fileResult.getLines().at(i) << endl;

		processData(fileResult.getLines().at(i));
	}

	return 0;
}
